import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;


public class Playgame extends JFrame {
	static JPanel contentPane;
	JLabel lblro;
	Vector GC;
	JLabel lblgc;
	private JTextField txtloai;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Playgame frame = new Playgame();
					frame.setVisible(false);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Playgame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(166, 308, 700, 500);
		contentPane = new JPanel();
		contentPane.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent event) {
				double a=event.getPoint().getX();
				lblro.setIcon(null);
				contentPane.add(lblro);
				lblro = new JLabel();
				try {
					BufferedImage trung=ImageIO.read(new File("Images\\Ro.png"));
					ImageIcon ictrung=new ImageIcon(trung.getScaledInstance(60,60, trung.SCALE_SMOOTH));
					lblro.setIcon(ictrung);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				lblro.setBounds((int)a-30, 393,(int) ( contentPane.WIDTH-a-30), (int)(contentPane.HEIGHT-393));
				contentPane.add(lblro);
				lblro.setSize(60,60);
				contentPane.add(lblro);
			}
		});
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblTho = new JLabel("th\u1EDDi gian");
		lblTho.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblTho.setBounds(37, 11, 54, 25);
		contentPane.add(lblTho);
		
		JLabel lblTrngG = new JLabel("S\u1ED1 Tr\u1EE9ng");
		lblTrngG.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblTrngG.setBounds(205, 11, 54, 25);
		contentPane.add(lblTrngG);
		
		JLabel lblCnLi = new JLabel("C\u00F2n l\u1EA1i");
		lblCnLi.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblCnLi.setBounds(340, 11, 54, 25);
		contentPane.add(lblCnLi);
		
		lblro = new JLabel();
		try {
			BufferedImage trung=ImageIO.read(new File("Images\\Ro.png"));
			ImageIcon ictrung=new ImageIcon(trung.getScaledInstance(60,60, trung.SCALE_SMOOTH));
			lblro.setIcon(ictrung);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		lblro.setBounds(300, 393, contentPane.WIDTH-300, contentPane.HEIGHT-320);
		contentPane.add(lblro);
		lblro.setSize(60,60);
		contentPane.add(lblro);
		
		JLabel lblToaDo = new JLabel("Nh\u1EADp Lo\u1EA1i th\u00EAm v\u00E0o");
		lblToaDo.setBounds(444, 17, 101, 14);
		contentPane.add(lblToaDo);
		
		txtloai = new JTextField();
		txtloai.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode()==arg0.VK_G)
				{
					TaoGc(1);
					txtloai.setText("");
				}
				if(arg0.getKeyCode()==arg0.VK_V)
				{
					TaoGc(2);
					txtloai.setText("");
				}
				if(arg0.getKeyCode()==arg0.VK_N)
				{
					TaoGc(3);
					txtloai.setText("");
				}
				
			}
		});
		txtloai.setBounds(542, 14, 86, 20);
		contentPane.add(txtloai);
		txtloai.setColumns(10);
		
		TaoGc(1);
	}
	public void TaoGc(final int loai)
	{
		Thread Gc=new Thread(){
			public void run(){
				lblgc = new JLabel();
				lblgc.setBounds(250, 40,250, (int)(contentPane.HEIGHT-40));
				contentPane.add(lblgc);
				lblgc.setSize(60,60);
				contentPane.add(lblgc);	
				int i;
				while(true)
				{
						for(i=0;i<690;i+=2){
							try {
								Thread.sleep(10);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							lblgc.setIcon(null);
							if(loai==1)
							{
								try {
									BufferedImage ga=ImageIO.read(new File("Images\\ga.png"));
									ImageIcon icga=new ImageIcon(ga.getScaledInstance(60,60, ga.SCALE_SMOOTH));
									lblgc.setIcon(icga);
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								lblgc.setBounds(i-30, 40,(int)(contentPane.WIDTH-i-30), (int)(contentPane.HEIGHT-40));
								contentPane.add(lblgc);
								lblgc.setSize(60,60);
								contentPane.add(lblgc);		
							}
							if(loai==2)
							{
								try {
									BufferedImage gc=ImageIO.read(new File("Images\\vit.png"));
									ImageIcon icgc=new ImageIcon(gc.getScaledInstance(60,60, gc.SCALE_SMOOTH));
									lblgc.setIcon(icgc);
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								lblgc.setBounds(i-30, 40,(int)(contentPane.WIDTH-i-30), (int)(contentPane.HEIGHT-40));
								contentPane.add(lblgc);
								lblgc.setSize(60,60);
								contentPane.add(lblgc);		
							}
							if(loai==3)
							{
								try {
									BufferedImage ga=ImageIO.read(new File("Images\\ga.png"));
									ImageIcon icgc=new ImageIcon(ga.getScaledInstance(60,60, ga.SCALE_SMOOTH));
									lblgc.setIcon(icgc);
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								lblgc.setBounds(i-30, 40,(int)(contentPane.WIDTH-i-30), (int)(contentPane.HEIGHT-40));
								contentPane.add(lblgc);
								lblgc.setSize(60,60);
								contentPane.add(lblgc);		
							}

					}

						
						for(i=690;i>=0;i-=2){
							try {
								Thread.sleep(10);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							lblgc.setIcon(null);
							if(loai==1)
							{
								try {
									BufferedImage ga=ImageIO.read(new File("Images\\ga.png"));
									ImageIcon icga=new ImageIcon(ga.getScaledInstance(60,60, ga.SCALE_SMOOTH));
									lblgc.setIcon(icga);
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								lblgc.setBounds(i-30, 40,(int)(contentPane.WIDTH-i-30), (int)(contentPane.HEIGHT-40));
								contentPane.add(lblgc);
								lblgc.setSize(60,60);
								contentPane.add(lblgc);		
							}
							if(loai==2)
							{
								try {
									BufferedImage gc=ImageIO.read(new File("Images\\vit.png"));
									ImageIcon icgc=new ImageIcon(gc.getScaledInstance(60,60, gc.SCALE_SMOOTH));
									lblgc.setIcon(icgc);
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								lblgc.setBounds(i-30, 40,(int)(contentPane.WIDTH-i-30), (int)(contentPane.HEIGHT-40));
								contentPane.add(lblgc);
								lblgc.setSize(60,60);
								contentPane.add(lblgc);		
							}
							if(loai==3)
							{
								try {
									BufferedImage ga=ImageIO.read(new File("Images\\ngan.png"));
									ImageIcon icgc=new ImageIcon(ga.getScaledInstance(60,60, ga.SCALE_SMOOTH));
									lblgc.setIcon(icgc);
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								lblgc.setBounds(i-30, 40,(int)(contentPane.WIDTH-i-30), (int)(contentPane.HEIGHT-40));
								contentPane.add(lblgc);
								lblgc.setSize(60,60);
								contentPane.add(lblgc);		
							}
							lblgc.setBounds(i-30, 40,(int)(contentPane.WIDTH-i-30), (int)(contentPane.HEIGHT-40));
							contentPane.add(lblgc);
							lblgc.setSize(60,60);
							contentPane.add(lblgc);		
						}
					trung(i);	
				}
				
			}
		
		};	
		Gc.start();
	}
	public void trung(final int td)
	{
		Thread makeegg=new Thread(){
			public void run(){
				double a=Math.random();
				String egg;
				if(a>0.2)
					egg="Images\\egg.png";
				else 
					egg="Images\\shit.png";
				lblgc = new JLabel();
				lblgc.setBounds(250, 40,250, (int)(contentPane.HEIGHT-40));
				contentPane.add(lblgc);
				lblgc.setSize(30,30);
				contentPane.add(lblgc);						

				for(int i=40;i<690;i+=2){
					try {
							Thread.sleep(30);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					lblgc.setIcon(null);
					try {
						BufferedImage trung=ImageIO.read(new File(egg));
						ImageIcon ictrung=new ImageIcon(trung.getScaledInstance(30,30, trung.SCALE_SMOOTH));
						lblgc.setIcon(ictrung);
					} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
					lblgc.setBounds(td, i-30,contentPane.HEIGHT-td, (int)(contentPane.HEIGHT-i-30));
					contentPane.add(lblgc);
					lblgc.setSize(60,60);
					contentPane.add(lblgc);		
				}			
			}
		};
		makeegg.run();
	}
}
